<?php	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class APIModel extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->library(array('session', 'pagination'));
        $this->load->helper('url');
        $this->load->database();
    }

    //创建PDO对象
    function get_pdo(){
        $host=$this->db->hostname;
        $database=$this->db->database;
        $dsn="mysql:dbname=$database;host=$host";
        $db_user=$this->db->username;
        $db_pass=$this->db->password;
        try{
            $pdo=new PDO($dsn,$db_user,$db_pass);
            $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);    //禁用模拟预处理语句,保证sql语句不被php解析
            $pdo->exec("set names utf8");
        }catch(PDOException $e){
            echo '数据库连接失败'.$e->getMessage();
        }
        $this->pdo=$pdo;

        //删改增
        //$sql=------------;
        //$this->pdo->exec($sql);
        //成功返回受影响行数
        //失败返回false
        //或者可以用checkLogin()中的绑定参数的方法
    }

    function checkLogin()
    {
        $this->get_pdo();
        $sNum=$this->input->post("sNum");
        $sPassWord=md5($this->input->post("sPassWord"));
        $sql="select * from students where sNum=? and sPassWord=?";
        $res="";
        $array=array("$sNum","$sPassWord");
        //$array=array("1427152073","123456");
        $stmt=$this->pdo->prepare($sql);
        $exeres = $stmt->execute($array);
        if($exeres){
            $res=$stmt->fetchAll(PDO::FETCH_ASSOC);
        }
        return $res;
    }
}